﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using System.IO;

namespace VodafoneContracts
{
    public class SmartZeroLimitConnection : CreditConnection
    {


        public SmartZeroLimitConnection(VodafoneResult result, string username, string password, string number, bool isFirstNumber, Action<HttpWebResponse> basicConnectionCallBack, Action onConnectionError, Action onCompletion)
            : base(result, username, password, number, isFirstNumber, basicConnectionCallBack, onConnectionError, null)
        {
            connections.AddConnection(new ConnectionGET()
                {
                    url = "https://www.areaprivati.vodafone.it/190/ebwe/Counters.do?ls=1&channelId=-18046&programId=536881486&pageTypeId=9604&tk=9604%2Cc&ty_key=fdt_pri_elenco_contatori_ric&ty_nocache=true",
                    CookieContainer = cookieJar,
                    prevProcessor = (_) => { },
                    currProcessor = (response) =>
                    {
                        var sr = new StreamReader(response.GetResponseStream());
                        var html = sr.ReadToEnd();
                        if (!html.Contains("Contatori"))
                        {
                            OnConnectionError();
                        }

                        BasicConnectionCallBack(response);

                    },
                    onError = OnConnectionError
                });

            connections.AddConnection(new ConnectionGET()
            {
                url = "https://www.areaprivati.vodafone.it/190/ebwe/InfoPackage3G.do?index=0",
                CookieContainer = cookieJar,
                prevProcessor = (_) => { },
                currProcessor = (response) =>
                {
                    var sr = new StreamReader(response.GetResponseStream());
                    var html = sr.ReadToEnd();
                    SmartZeroLimitsParser parser = new SmartZeroLimitsParser(html);
                    try
                    {
                        result.traffic = parser.getResult();
                        onCompletion();
                    }
                    catch
                    {
                        OnConnectionError();
                    }
                },
                onError = OnConnectionError
            });
        }
    }

}

